//
//  BRRentalOrderListCell.m
//  Kexu
//
//  Created by IOS on 2023/7/31.
//

#import "BRRentalOrderListCell.h"

@implementation BRRentalOrderListCell


- (void)setDto:(BROrderListModel *)dto {
    _dto = dto;
    
    NSDecimalNumber *money = dto.realAmount;
    if (dto.state == 5) {
        money = dto.orderAmount;
    }else if (dto.state == 2) {
        money = dto.money;
    }
    
    self.amountL.text = [BRPayManager br_amount:money];
    
    self.orderIdL.text = [NSString stringWithFormat:@"%@: %@",Localized(@"shop_orderID"),dto.orderNo];
    self.timeL.text = [BRTools getTimeFromTimestamp:dto.createTime.integerValue formatter:kTimeFormatter];
    NSInteger state = dto.state;

    [self.stateView br_status:state];
    
}

#pragma mark - UI --
- (void)setUI {
    [super setUI];
    
    self.contView.backgroundColor = UIColor.whiteColor;
    [self.contView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.offset(-10);
    }];
    
    [self.contView addSubview:self.stateView];
    [self.stateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.top.offset(12);
        make.height.mas_equalTo(24);
        make.right.offset(0);
    }];
    
    self.amountL = [BRToolsView br_label:@"0.000" textColor:kColorTitle font:[UIFont fontB:20]];
    [self.contView addSubview:self.amountL];
    [self.amountL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.right.offset(-20);
    }];
    
    UIStackView *stack = [[UIStackView alloc] init];
    stack.axis = UILayoutConstraintAxisVertical;
    stack.spacing = 6;
    [self.contView addSubview:stack];
    [stack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.top.offset(48);
        make.right.offset(0);
        make.bottom.offset(-14);
    }];
    
    self.orderIdL = [BRToolsView br_label:@"--" textColor:UIColorMakeWithHex(@"#88666B") font:[UIFont fontM:13]];
    self.timeL = [BRToolsView br_label:@"--" textColor:UIColorMakeWithHex(@"#88666B") font:[UIFont fontM:13]];
    [stack addArrangedSubview:self.orderIdL];
    [stack addArrangedSubview:self.timeL];
}

- (BRStateView *)stateView {
    
    if (_stateView == nil) {
        
        _stateView = [[BRStateView alloc] init];
    }
    
    return _stateView;
}

@end

@implementation BRTRListCell

- (void)setDto:(BRBrokeragesListModel *)dto {
    _dto = dto;
    
    NSString *symol = dto.type == 3 ? @"-" : @"+";
    double amount = dto.amount.doubleValue; // 取绝对值
    NSDecimalNumber *money = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%f",fabs(amount)]];
    self.amountL.text = [NSString stringWithFormat:@"%@ %@",symol,[BRPayManager br_amount:money]];
    
    self.titleL.text = [NSString stringWithFormat:@"%@: %@",Localized(@"order_orderID"),dto.orderNo];
    self.textL.text = [BRTools getTimeFromTimestamp:dto.createTime.integerValue formatter:kTimeFormatter];
    
    //type    类型：1-订单分成 2-调整 3-退款    integer(int32)
    self.stateL.text = dto.type == 3 ? Localized(@"shop_rentalRefund") : Localized(@"shop_rentalProfit");
}

#pragma mark - UI --
- (void)setUI {
    [super setUI];
    
    self.contView.backgroundColor = UIColor.whiteColor;
    [self.contView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.offset(-10);
    }];
    
    UIStackView *stack = [[UIStackView alloc] init];
    stack.axis = UILayoutConstraintAxisVertical;
    stack.spacing = 10;
    [self.contView addSubview:stack];
    [stack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.top.offset(14);
        make.right.offset(0);
        make.bottom.offset(-14);
    }];
    
    self.titleL = [BRToolsView br_label:@"--" textColor:kColorTitle font:[UIFont fontM:14]];
    self.textL = [BRToolsView br_label:@"--" textColor:kColorTitle66B font:[UIFont fontM:13]];
    [stack addArrangedSubview:self.titleL];
    [stack addArrangedSubview:self.textL];
    
    
    self.amountL = [BRToolsView br_label:@"0.000" textColor:kColorTitle font:[UIFont fontB:15]];
    self.stateL = [BRToolsView br_label:@"--" textColor:kColorTitle66B font:[UIFont fontM:13]];
    
    self.amountL.textAlignment = NSTextAlignmentRight;
    self.stateL.textAlignment = NSTextAlignmentRight;
    
    UIStackView *stackRight = [[UIStackView alloc] init];
    stackRight.axis = UILayoutConstraintAxisVertical;
    stackRight.spacing = 4;
    [self.contView addSubview:stackRight];
    [stackRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.right.offset(-14);
    }];
    
    [stackRight addArrangedSubview:self.amountL];
    [stackRight addArrangedSubview:self.stateL];
    
}

@end


@implementation BRWRecordListCell

- (void)setDto:(BRWithdrawasRecordModel *)dto {
    _dto = dto;
    
    self.hasRemark = dto.state == 2;
    
    self.textL.text = [NSString stringWithFormat:@"%@",[BRTools getTimeFromTimestamp:dto.createTime.integerValue formatter:kTimeFormatter]];
    self.amountL.text = [BRPayManager br_amount:dto.amount];
    
    
    self.remarkL.text = dto.remark;
    
    [self.remarkL sizeToFit];
    
    
    [self.stateView br_statusByWithdrawalRecord:dto.state];
}

- (void)toOpenRemark {
    
    if (self.openBlock) self.openBlock(YES);
    self.open = !self.open;
    if (self.openBlock) self.openBlock(NO);
}


- (void)setHasRemark:(BOOL)hasRemark {
    _hasRemark = hasRemark;
    
//    CGFloat b = hasRemark ? 58 : 14;
    CGFloat h = hasRemark ? 113 : 73;
    
//    [self.textL mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.bottom.offset(-b);
//    }];
    
    [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(h);
    }];
    
    self.remarkView.hidden = !hasRemark;
}

- (void)setOpen:(BOOL)open {
    _open = open;
    
    self.bottomView.hidden = !open;
    WS(weakSelf);
    if (open) {
        
        [UIView animateWithDuration:0.25 animations:^{
            
            weakSelf.img.transform = CGAffineTransformMakeRotation(M_PI);
            self.bottomView.alpha = 1;
        } completion:^(BOOL finished) {
            
        }];
        
    }else {
//        self.bottomView.hidden = !open;
        [UIView animateWithDuration:0.25 animations:^{
            weakSelf.img.transform = CGAffineTransformMakeRotation(0);
            self.bottomView.alpha = 0;
        } completion:^(BOOL finished) {
            
        }];
    }
    
   
}

#pragma mark - UI --
- (void)setUI {
    [super setUI];
    
    self.contView.backgroundColor = UIColor.whiteColor;
    [self.contView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.offset(-10);
    }];
    
    UIStackView *stack = [[UIStackView alloc] init];
    stack.axis = UILayoutConstraintAxisVertical;
    [self.contView addSubview:stack];
    [stack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
        
    }];
    
    self.topView = [[UIView alloc] init];
    self.bottomView = [[UIView alloc] init];
    
    [stack addArrangedSubview:self.topView];
    [stack addArrangedSubview:self.bottomView];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(73);
    }];
    
    [self.topView addSubview:self.stateView];
    [self.stateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.top.offset(12);
        make.height.mas_equalTo(24);
        make.right.offset(0);
    }];
    
    self.amountL = [BRToolsView br_label:@"0.000" textColor:kColorTitle font:[UIFont fontB:17]];
    [self.topView addSubview:self.amountL];
    [self.amountL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.stateView.mas_centerY).offset(0);
        make.right.offset(-20);
    }];
    
    self.textL = [BRToolsView br_label:@"--" textColor:UIColorMakeWithHex(@"#88666B") font:[UIFont fontM:13]];
    [self.topView addSubview:self.textL];
    [self.textL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.top.offset(44);
        make.right.offset(0);
//        make.bottom.offset(-14);
    }];
    
    self.remarkView = [[UIView alloc] init];
    [self.topView addSubview:self.remarkView];
    [self.remarkView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.mas_equalTo(44);
        make.top.offset(73);
    }];
    
    
    UIView *line = [BRToolsView getLineView];
    [self.remarkView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.right.offset(-14);
        make.height.mas_equalTo(0.5);
        make.top.offset(0);
    }];
    
    QMUILabel *remarkL = [BRToolsView br_label:Localized(@"shop_remarks") textColor:kColorTitle font:[UIFont fontS:13]];
    [self.remarkView addSubview:remarkL];
    [remarkL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.centerY.offset(0);
    }];
    
    [self.remarkView addSubview:self.img];
    [self.img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(6);
        make.right.offset(-14);
        make.centerY.offset(0);
    }];
    
    
    UIControl *openBtn = [[UIControl alloc] init];
    [openBtn addTarget:self action:@selector(toOpenRemark) forControlEvents:UIControlEventTouchUpInside];
    [self.remarkView addSubview:openBtn];
    [openBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    self.bottomView.hidden = YES;
    QMUILabel *remarkL2 = [BRToolsView br_label:@"--" textColor:kColorTitle font:[UIFont fontM:13]];
    remarkL2.numberOfLines = 0;
    [self.bottomView addSubview:remarkL2];
    [remarkL2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.offset(14);
        make.bottom.right.offset(-14);
    }];
    _remarkL = remarkL2;
}

- (BRStateView *)stateView {
    
    if (_stateView == nil) {
        
        _stateView = [[BRStateView alloc] init];
    }
    
    return _stateView;
}

- (UIImageView *)img {
    if (_img == nil) {
        _img = [[UIImageView alloc] initWithImage:UIImageMake(@"arrow_down_gray10")];
    }
    return _img;
}


@end
